package com.myccb.app.widget;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.myccb.app.R;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by hyt on 2017/12/7.
 */

public class LoadingCountdownDialog extends Dialog {

    private ImageView loadingImg;
    private TextView countdownText;
    private Timer countdownTimer;
    private Handler countdownHandler;
    private int countdownTime;

    public LoadingCountdownDialog(@NonNull Context context) {
        super(context,R.style.LoadingCountdownDialogTheme);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loading_countdown_dialog_layout);
        setCanceledOnTouchOutside(false);
        initView();
    }

    private void initView() {
        loadingImg = findViewById(R.id.imageview_loading);
        countdownText = findViewById(R.id.textview_countdown);
    }

    @SuppressLint("HandlerLeak")
    private void startCountdown(int countTime) {
        countdownTime=countTime;
        countdownHandler = new Handler() {
            public void handleMessage(Message msg) {
                if (!isShowing()){
                    countdownTime=0;
                    countdownTimer.cancel();
                }
                if (msg.what > 0) {
                    countdownText.setText(""+msg.what);
                    rotateLoadingImg();
                } else {
                    if (isShowing()) {
                        dismiss();
                    }
                    countdownTimer.cancel();
                }
                super.handleMessage(msg);
            }
        };
        countdownTimer = new Timer(true);
        TimerTask timerTask = new TimerTask() {
            //int countTime = 10;
            public void run() {
                if (countdownTime > 0) {
                    countdownTime--;
                }
                Message msg = new Message();
                msg.what = countdownTime;
                countdownHandler.sendMessage(msg);
            }
        };
        countdownTimer.schedule(timerTask, 1000, 1000);
    }
    private int fromDegrees=0;
    private void rotateLoadingImg(){
        Animation rotateAnimation  = new RotateAnimation(fromDegrees, fromDegrees+180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotateAnimation.setFillAfter(true);
        rotateAnimation.setDuration(1000);
        rotateAnimation.setRepeatCount(0);
        rotateAnimation.setInterpolator(new LinearInterpolator());
        loadingImg.startAnimation(rotateAnimation);
        fromDegrees=(fromDegrees+180)%360;
    }
    public void show(int countTime) {
        super.show();
        startCountdown(countTime);
    }
}